/* Copyright 2008. All rights reserved. */
package org.bling.parser.tags;

import org.bling.parser.BlingParserException;
import org.bling.util.Util;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Widget;

/**
 * renders an SWT Link
 * 
 * @author DISBROB
 * 
 */
public class LinkTag extends Tag {

	protected Link link;
	protected String href;

	public void setHref(String href) {
		this.href = href;
	}

	@Override
	public int doStartTag() throws BlingParserException {
		super.doStartTag();

		if (Util.isBlankOrNull(href)) {
			throw new BlingParserException("HREF Required");
		}

		if (!(parent.getWidget() instanceof Composite)) {
			throw new BlingParserException(
					"Label tags can only be inside of composites");
		}
		Composite composite = (Composite) parent.getWidget();
		link = new Link(composite, SWT.NONE);
		link.setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_HAND));

		return EVAL_PAGE;
	}

	@Override
	public int doAfterBody() throws BlingParserException {
		super.doAfterBody();
		link.setText("<a href=\"" + href + "\">" + bodyContent + "</a>");
		return EVAL_PAGE;
	}

	@Override
	public Widget getWidget() throws BlingParserException {
		return link;
	}

}
